<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
		<!-- 搜索开始 -->
        <fieldset class="layui-elem-field layuimini-search">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="searchForm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">客户名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="customername" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">联系人</label>
                            <div class="layui-input-inline">
                                <input type="text" name="connectionperson" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">联系电话</label>
                            <div class="layui-input-inline">
                                <input type="text" name="phone" id="phone"  autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        	<div class="layui-form-item">
								<div class="layui-block" style="text-align: center;">
									<a class="layui-btn " lay-submit=""  lay-filter="data-search-btn"><label class="layui-icon layui-icon-search"></label>搜索</a>
									<a class="layui-btn layui-btn-warm" onclick="javascript:$('#searchForm')[0].reset()" lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
								</div>
                            </div>
                    </div>
                </form>
            </div>
        </fieldset>
		<!-- 搜索开始 -->
		
		<script type="text/html" id="availableTpl">
			  <!-- 这里的 checked 的状态只是演示 -->
			  <input type="checkbox" name="available" value="{{d.id}}" lay-skin="switch" lay-text="可用|不可用" lay-filter="availableSwitch" {{ d.available == 1 ? 'checked' : '' }}>
		</script>
				
	<!--表格开始-->
        <table class="layui-hide" id="customerTable" lay-filter="customerTable"></table>
        <div style="display: none;" id="customerToolBar">
			<a class="layui-btn layui-btn-sm " lay-event="addCustomer">添加</a>
            <a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="batchdelete">批量删除</a>
        </div>
		<div style="display: none;" id="customerTableBar">
			 <a class="layui-btn layui-btn-xs layui-btn-warm   data-count-delete" lay-event="update">修改</a>
		     <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
		</div>
		<!--表格结束-->
		
		<!-- 增加修改弹出层开始 -->
		<div id="addOrUpdateDiv" style="display: none; padding: 0.375rem;" >
			<form method="post" class="layui-form layui-form-pane" lay-filter="dataFrm" id="dataFrm">
				<div class="layui-form-item">
				    <div class="layui-inline">
				       <label class="layui-form-label">客户名称</label>
				       <div class="layui-input-inline">
						   <input type="hidden" name="id" />
				         <input type="text" name="customername" lay-verify="required" placeholder="请输入客户名称" autocomplete="off" class="layui-input">
				       </div>
				    </div>
				    <div class="layui-inline">
				       <label class="layui-form-label">公司电话</label>
				       <div class="layui-input-inline">
				         <input type="text" name="telephone" id="telephone"  placeholder="请输入公司电话" autocomplete="off" class="layui-input">
				       </div>
				    </div>
				    <div class="layui-inline">
				       <label class="layui-form-label">邮编</label>
				       <div class="layui-input-inline">
				         <input type="text" name="zip" placeholder="请输入邮编" autocomplete="off" class="layui-input">
				       </div>
				    </div>
				</div>
			    <div class="layui-form-item">
				   <label class="layui-form-label">公司地址 </label>
				   <div class="layui-input-block">
					 <input type="text" name="address" lay-verify="title" autocomplete="off" placeholder="请输入公司地址" class="layui-input">
				   </div>
				</div>
				
				<div class="layui-form-item">
				    <div class="layui-inline">
				       <label class="layui-form-label">联系人</label>
				       <div class="layui-input-inline">
				         <input type="text" name="connectionperson" lay-verify="required" placeholder="请输入联系人" autocomplete="off" class="layui-input">
				       </div>
				    </div>
				    <div class="layui-inline">
				       <label class="layui-form-label">联系电话</label>
				       <div class="layui-input-inline">
				         <input type="text" name="phone" id="phone"  placeholder="请输入联系电话" autocomplete="off" class="layui-input">
				       </div>
				    </div>
				    <div class="layui-inline">
				       <label class="layui-form-label">邮箱</label>
				       <div class="layui-input-inline">
				         <input type="text" name="email" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
				       </div>
				    </div>
				</div>
				
				<div class="layui-form-item">
				    <div class="layui-inline">
				       <label class="layui-form-label">开户银行</label>
				       <div class="layui-input-inline">
				         <input type="text" name="bank" lay-verify="required" placeholder="请输入开户银行" autocomplete="off" class="layui-input">
				       </div>
				    </div>
				    <div class="layui-inline">
				       <label class="layui-form-label">账号</label>
				       <div class="layui-input-inline">
				         <input type="text" name="account" id="account"  placeholder="请输入账号" autocomplete="off" class="layui-input">
				       </div>
				    </div>
				    <div class="layui-inline">
				       <label class="layui-form-label">传真</label>
				       <div class="layui-input-inline">
				         <input type="text" name="fax" placeholder="请输入传真" autocomplete="off" class="layui-input">
				       </div>
				    </div>
				</div>
				  
				  <div class="layui-form-item">
				    <div class="layui-input-block" style="text-align: center;">
						<a class="layui-btn " lay-submit=""  lay-filter="doSubmit"><label class="layui-icon layui-icon-release"></label>提交</a>
						<a class="layui-btn layui-btn-warm" onclick="javascript:$('#dataFrm')[0].reset()" lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
				    </div>
				  </div>
				  
			</form>
		</div>
		<!-- 增加修改弹出层结束 -->
		
		
		
    </div>
</div>
<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../lib/common/common.js " charset="utf-8"></script>

<script>
    layui.use(['form', 'table','laydate','layer'], function () {
        var form = layui.form;
        var table = layui.table;
		var laydate = layui.laydate;
		var layer = layui.layer;

     var tableObj=  table.render({
            elem: '#customerTable',
            url: api+'customer/loadAllCustomer',
		    toolbar:"#customerToolBar",
            cols: [[
               {type: "checkbox",  fixed: "left" ,width:80},
                              {field: 'id', title: 'ID', align: "center",width:80},
                              {field: 'customername',  title: '客户名称', align: "center",width:120},
                              {field: 'telephone',  title: '公司电话', align: "center",width:150},
                              {field: 'connectionperson',  title: '联系人', align: "center",width:120},
                              {field: 'phone',  title: '联系人电话', align: "center",width:150},
                              {field: 'zip',  title: '邮编', align: "center",width:150},
                              {field: 'address',  title: '地址', align: "center",width:150},
                              {field: 'bank',  title: '开户行', align: "center",width:180},
                              {field: 'account',  title: '账号', align: "center",width:180},
                              {field: 'email',  title: '邮箱', align: "center",width:180},
                              {field: 'fax',  title: '传真', align: "center",width:120},
                              {field: 'available',  title: '是否可用', align: "center",width: 120,templet:'#availableTpl'},
                              {title: '操作', templet: '#customerTableBar', fixed: "right", align: "center",width:180}
            ]],
            page: true
        });
		
		
		// 监听搜索操作
		form.on('submit(data-search-btn)', function (data) {			
			var params=$("#searchForm").serialize();
		    //执行搜索重载
		    table.reload('customerTable', {
		        page: {
		            curr: 1
		        },
				 url: api+'customer/loadAllCustomer?'+params,
		       
		    });
		    return false;
		});
		
		//打开弹出层
		var mainIndex;
		var url;
		function openAddCustomer(){
			mainIndex=layer.open({
				 type: 1,
				 title:'添加客户',
				 content:$("#addOrUpdateDiv"),
				 area:['800px','600px'],
				 success:function(index){
					 url=api+"customer/addCustomer";
					 $("#dataFrm")[0].reset();
					  // editor.txt.html("");
				 }
			});
		}
		
		function openUpdateCustomer(data){
			mainIndex=layer.open({
				 type: 1,
				 title:'修改['+data.title+']客户',
				 content:$("#addOrUpdateDiv"),
				 area:['800px','600px'],
				 success:function(index){
					 url=api+"customer/updateCustomer";
					 //装载数据表格 
					 form.val("dataFrm",data);
					 //设置富文本的内容
					 // editor.txt.html(data.content);
				 }
			});
		}
		
		
		
		//监听是否可用事件
		form.on('switch(availableSwitch)', function(obj){
				  $.post(api+"customer/updateCustomer",{id:this.value,available:obj.elem.checked?1:0},function(res){
				  	layer.msg(res.msg);
				  	tableObj.reload();
				  });
		  });
		
		
		
		// 提交数据
		form.on("submit(doSubmit)",function(data){
		    var params=$("#dataFrm").serialize();
			// var content=editor.txt.html();
			// params+="&content="+content;
			$.post(url,params,function(res){
				layer.msg(res.msg);
				if(res.code==200){
					tableObj.reload();
				}
				//关闭弹出层
				layer.close(mainIndex);
			})
			
			return false;
		})




  

     table.on('toolbar(customerTable)', function(obj){
       var checkStatus = table.checkStatus(obj.config.id);
	   
       switch(obj.event){
         case 'batchdelete':
		 //批量删除
		 batchDelete();
         break;
		 case 'addCustomer':
		 openAddCustomer();
		 break;
		 
        
       };
     });

    
		//单行删除
        table.on('tool(customerTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
					$.post(
					api+"customer/deleteCustomer",
					{id:data.id},
				function(res){
					 layer.msg(res.msg);
					 obj.del();
					 layer.close(index);
					})                 
                });	
            }else if(obj.event==="update"){
				openUpdateCustomer(data);
			}
        });
		
		
		function batchDelete(){
			var checkStatus = table.checkStatus('customerTable'); //idTest 即为基础参数 id 对应的值
			var data=checkStatus.data;
			var length=checkStatus.data.length;
			if(length==0){
						 layer.msg("请选择所要删除的行")
			}else{
						 layer.confirm('真的删除行么', function (index) {
							 var params="";
							 $.each(data,function(index,item){
								 if(index=0){
								 	 params+="ids="+item.id;			 
								 }else{
									 params+="&ids="+item.id; 
								 }
							 });
							 $.post(
							 api+"customer/batchDeleteCustomer",
							 params,
							 function(res){
								 layer.msg(res.msg);
								 tableObj.reload();
								  layer.close(index);
							 })						    
						 });
			}
		}

			
		
		

		
		
    });
</script>
<script>

</script>

</body>
</html>